package com.tanhua.server.controller;

import com.tanhua.model.vo.ErrorResult;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 登录注册控制层
 * 前后端分离架构中：接口请求路径 参数 方式 响应结果 要严格按照接口文档编写
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 第一步：登录注册-获取验证码
     * ResponseEntity：响应实体对象 由spring提供（状态码 成功或错误信息等返回）
     * Body:请求头{"phone":"13311111"} Query:form表单或请求地址带过来的参数
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResponseEntity sendCode(@RequestBody Map<String,String> params){
        String phone = params.get("phone");//获取手机号码
        userService.sendCode(phone);
        return ResponseEntity.ok(null);
    }

    /**
     * 第二步：登录注册实现
     */
    @RequestMapping(value = "/loginVerification",method = RequestMethod.POST)
    public ResponseEntity login(@RequestBody Map<String,String> params){
        String phone = params.get("phone"); //手机号
        String verificationCode = params.get("verificationCode");//验证码
        Map map = userService.login(phone,verificationCode);//map ==> {“token”：“xxxxx”,isNew:true}
        return ResponseEntity.ok(map);
    }
}
